﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MySQLApplication
{
    class cmd_Export : Command
    {
        CSVWriter csvWriter = new CSVWriter();

        string[] str_supportedFileTypes; // Normally I'd use an enumerator, but for what I want to do, a string array would be much easier.
        public cmd_Export()
        {
            str_command = "export";
            str_description = @"This command is used to export MySQL databases to various file formats.
                                Usage Example: export users csv_users csv
                                Current supported formats are:";
            str_supportedFileTypes = new string[] { "csv" };
            str_paramNames = new string[]{"table", "output file name (without extension)", "output file type"};
        }
        public override void onCall(string[] str_parameters)
        {
            if (str_parameters.Length != str_paramNames.Length + 1) // Basically, if the number of arguments (the +1 being the command itself) aren't equal 
            {
                Console.WriteLine("Incorrect syntax.");
                if (str_parameters.Length > str_paramNames.Length)
                {
                    Console.WriteLine("Too many parameters.");
                }
                else
                {
                    Console.WriteLine("Not enough parameters.");
                }
                getSyntax();
                return;
            }
            bool bl_fileTypeFound = false;
            for (int i = 0; i < str_supportedFileTypes.Length; i++)
            {
                if (str_parameters[3].Equals(str_supportedFileTypes[i]))
                {
                    bl_fileTypeFound = true;
                }
            }
            if (!bl_fileTypeFound)
            {
                Console.WriteLine("File type " + str_parameters[2] + " not found.  Here are the currently supported file types:");
                for (int i = 0; i < str_supportedFileTypes.Length; i++)
                {
                    Console.WriteLine(str_supportedFileTypes[i]);
                }
            }
            csvWriter.write(str_parameters[1], str_parameters[2]);


        }
        public override void getSyntax()
        {
            base.getSyntax();
            for (int i = 0; i < str_supportedFileTypes.Length; i++)
            {
                Console.WriteLine(str_supportedFileTypes[i]);
            }
            Console.WriteLine("You can include a full file path in the \"output file name\" field.  Example:  C:\\Kevin\\fileName will create fileName.csv");
        }
    }
}
